﻿
    using System;
    using System.IO;

namespace YoucaiReader.ImageTool
{
    public class BufferedStream : Stream
    {
        private Stream _baseStream;
        private byte[] _buffer;
        private int _position;

        public BufferedStream(Stream baseStream, int length)
        {
            Guard.NotNull(baseStream, "baseStream");
            this._baseStream = baseStream;
            Guard.GreaterThan<int>(length, 0, "length");
            this._buffer = new byte[length];
            this._baseStream.Read(this._buffer, 0, length);
        }

        public override void Flush()
        {
            this._baseStream.Flush();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            Guard.NotNull(buffer, "buffer");
            Guard.GreaterThan<int>(buffer.Length, (offset + count) - 1, "offset");
            Guard.GreaterThan<int>(count, 0, "count");
            Guard.GreaterEquals<int>(offset, 0, "offset");
            int num = 0;
            int num2 = count;
            int length = 0;
            if (this._position < this._baseStream.Position)
            {
                length = this._buffer.Length - this._position;
                if (length > count)
                {
                    length = count;
                }
                num2 -= length;
                num += length;
                Array.Copy(this._buffer, this._position, buffer, offset, length);
            }
            if (num2 > 0)
            {
                num += this._baseStream.Read(buffer, offset + length, num2);
            }
            this._position += count;
            return num;
        }

        public void Reset()
        {
            this._position = 0;
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotImplementedException();
        }

        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotImplementedException();
        }

        public override bool CanRead
        {
            get
            {
                return this._baseStream.CanRead;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return this._baseStream.CanSeek;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return this._baseStream.CanWrite;
            }
        }

        public override long Length
        {
            get
            {
                return this._baseStream.Length;
            }
        }

        public override long Position
        {
            get
            {
                return (long) this._position;
            }
            set
            {
                throw new NotSupportedException();
            }
        }
    }
}

